package com.fingard.dsp.bank.directbank.cbhb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.net.ConnState;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

public class CBHB02QryRefund extends CBHB02Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        int tmpSuccCount = 0;
        double tmpTotalAmt = 0;

        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req.getQryStartDate());
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req.getQryEndDate());
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按七天循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否大于十天
                if (DateHelper.getDifferDays(tmpCalendarTo.getTime(), tmpCalendarFrom.getTime()) > 10) {
                    tmpClndrStepTo.set(Calendar.DATE, 10);
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                CBHB02Header bankHeader = new CBHB02Header();
                bankHeader.transType = "SCRQ";
                bankHeader.instSeq = getQryTransID();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                xmlWriter.autoFormat = false;
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeElementString("pyrAct", p_req.payAct);
                xmlWriter.writeElementString("sysCd", "B");
                xmlWriter.writeElementString("begDat", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                xmlWriter.writeElementString("endDat", Format.Date8Format.format(tmpClndrStepTo.getTime()));
                xmlWriter.writeStartDocByDeclaration("</" + bankHeader.transType + "Req>");
                xmlWriter.writeEndElement();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        while (tmpBkReader.read("/Cbhbpac/Message/"+bankHeader.transType+"Res/tranList")) {
                            RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();
                            String refundTicketDate = tmpBkReader.readedXElement.getSingleText("bcnDat");
                            tmpRetDetail.refundTicketDate = refundTicketDate.substring(0, 4) + "-" + refundTicketDate.substring(4, 6) + "-" + refundTicketDate.substring(6);
                            tmpRetDetail.amount = tmpBkReader.readedXElement.getSingleText("txnAmt");
                            tmpRetDetail.refundTicketReason = tmpBkReader.readedXElement.getSingleText("rtnInf");
                            tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("oPyeAct");
                            tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("oPyrAct");
                            tmpRetDetail.askNum = tmpBkReader.readedXElement.getSingleText("serialNo");
                            tmpSuccCount++;
                            tmpTotalAmt += Double.parseDouble(tmpRetDetail.amount);

                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    tmpRespCode = "0";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }

            retAts.totleAmount = String.valueOf(tmpTotalAmt);
            retAts.totleNum = String.valueOf(tmpSuccCount);
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if (tmpRespCode.equals("-1")) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("当前时间范围内无退票数据");
        }

        retAts.writeEnd();
        return retAts;
    }
}
